package com.jsj.servlet.gb;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jsj.dao.IRoleDao;
import com.jsj.dao.IRoleDao;
import com.jsj.dao.impl.RoleDaoImpl;
import com.jsj.dao.impl.RoleDaoImpl;
import com.jsj.model.Role;
import com.jsj.utils.StringUtils;

/**
 * 负责完成用户信息的CRUD、登录、注销等功能
 * @author Administrator
 *
 */

@WebServlet("/role")
public class RoleServlet extends HttpServlet {

	private static final long serialVersionUID = -3704200856467333570L;
	
	private IRoleDao roleDao=new RoleDaoImpl();
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");		
		PrintWriter out=response.getWriter();
		HttpSession session = request.getSession();
		
		String type=request.getParameter("type");
		
		String rolename=request.getParameter("rolename");
		
		
		Boolean flag=false;
		
		int ret=0;
		
		if(type.equals("getAll")) {
	    	getAll(request, response);	    	
	    }else if(type.equals("add")) {
	    	add(request, response, rolename);
	    }else if(type.equals("get")) {
	    	get(request, response);
	    }else if(type.equals("edit")) {
	    	edit(request, response, rolename);
	    }else if(type.equals("del")) {
	    	delete(request, response);
	    }
	    
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int ret;
		String id=request.getParameter("id");
		ret=roleDao.delete(Integer.parseInt(id));
		if(ret!=0) {
			response.sendRedirect(request.getContextPath()+"/role?type=getAll&page=1");
		}
	}

	private void edit(HttpServletRequest request, HttpServletResponse response, String rolename)
			throws IOException {
		int ret;
		String id=request.getParameter("id");
		Role u=new Role();
		u.setRoleName(rolename);		
		u.setId(Integer.parseInt(id));
		
		ret=roleDao.edit(u);
		if(ret!=0) {
			response.sendRedirect(request.getContextPath()+"/role?type=getAll&page=1");
		}
	}

	private void get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		Role u=roleDao.get(Integer.parseInt(id));
		request.setAttribute("role", u);
		RequestDispatcher dispatcher=request.getRequestDispatcher("admin/role/editrole.jsp");
		dispatcher.forward(request,response);
	}

	private void add(HttpServletRequest request, HttpServletResponse response, String rolename)
			throws IOException {
		int ret;
		Role u=new Role();
		u.setRoleName(rolename);		
		ret=roleDao.add(u);
		if(ret!=0) {
			response.sendRedirect(request.getContextPath()+"/role?type=getAll&page=1");
		}
	}

	private void getAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String p=request.getParameter("page");
		int page;
		try {
			page=Integer.valueOf(p);
		} catch (Exception e) {
			page=1;
		}
		//每页记录数
		int pageSize=3;
		
		//某页开始记录索引
		int beginIndex=(page-1)*pageSize;
		
		List<Role> roleList=roleDao.getAll(page,pageSize);
		
		
		String sql="select COUNT(*) from message where 1=1";
		
		Object[] params=new Object[] {};
		
		//总记录数
		int totalRecords=roleDao.count(sql,params);		
		//总页数
		int totalPages=totalRecords % pageSize ==0 ? totalRecords / pageSize : totalRecords / pageSize +1;
		//某页结束的记录索引
		int endIndex=beginIndex+pageSize;  
		if(endIndex>totalRecords) {
			endIndex=totalRecords;
		}
		
		request.setAttribute("page", page);
		request.setAttribute("totalRecords", totalRecords);
		request.setAttribute("pageSize", pageSize);
		request.setAttribute("totalPages", totalPages);
		request.setAttribute("beginIndex", beginIndex);
		request.setAttribute("endIndex", endIndex);
		request.setAttribute("roleList", roleList);
		
		RequestDispatcher dispatcher=request.getRequestDispatcher("admin/role/roles.jsp");
		dispatcher.forward(request,response);
	}

	
	
	@Override
	protected void doPost(HttpServletRequest requset, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(requset, response);
	}
	

}
